﻿/*
 * GNU GENERAL PUBLIC LICENSE v2
 * 
 * Developers: Alan Audette, Oguz Bastemur
 */
namespace SqlDatabaseBackup.Helper
{
    using System;
    using System.Diagnostics;
    using System.IO;

    internal static class FileSystemHelper
    {
        internal static bool CreateFolders(string BackupFolder)
        {
            string[] dirs = BackupFolder.Split('\\');

            bool pathContainsDriveLetter = false;

            string currentFolder = "";
            if (dirs[0].Length == 1)
            {
                pathContainsDriveLetter = true;
                currentFolder = dirs[0] + ":\\";
            }

            //skip first and last items (first: drive name and last empty)
            for (int i = Convert.ToInt16(pathContainsDriveLetter); i < dirs.Length - 1; i++)
            {
                if (dirs[i].Trim() == "")
                    continue;
                currentFolder += dirs[i] + "\\";
                if (!Directory.Exists(currentFolder))
                {
                    try
                    {
                        Directory.CreateDirectory(currentFolder);
                    }
                    catch
                    {
                        Log.Message("CreateDirectory Failed: " + currentFolder, EventLogEntryType.Error);
                        Program.processFailed = true;
                        return false;
                    }
                }
            }
            return true;
        }
    }
}